package com.yuhang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuhang.entity.*;
import com.yuhang.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/hy-welcome")
public class WelcomeController {

    @Autowired
    private HyBuildService hyBuildService;

    @Autowired
    private HyUserService hyUserService;

    @Autowired
    private HyComplaintsService hyComplaintsService;

    @Autowired
    private HyActivityService hyActivityService;

    @Autowired
    private HyRepairStreamService hyRepairStreamService;

    @Autowired
    private HyBillService hyBillService;

    @RequestMapping("/selectBuildAndUser")
    public Map<String,Object> selectBuildAndUser(){
        QueryWrapper<HyBuild> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fid",0);
        List<HyBuild> hyBuildList1 = hyBuildService.list(queryWrapper);
        List<HyBuild> hyBuildList2 = hyBuildService.list();
        List<HyUser> hyUserList = hyUserService.list();
        BigDecimal decimal1 = new BigDecimal(hyUserList.size());
        BigDecimal decimal2 = new BigDecimal(3*(hyBuildList2.size()-hyBuildList1.size()*3));
        BigDecimal bigDecimal = decimal1.divide(decimal2,4, RoundingMode.HALF_UP);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("zds",hyBuildList1.size());
        map.put("zhs",3*(hyBuildList2.size()-hyBuildList1.size()*3));
        map.put("zrs",hyUserList.size());
        map.put("kzl",bigDecimal);
        return map;
    }

    @RequestMapping("/selectByMonth")
    public List<StateMonth> selectByMonth() {

        List<StateMonth> list = hyComplaintsService.selectByMonth();
        return list;
    }
    @RequestMapping("/selectByMonth2")
    public List<StateMonth> selectByMonth2() {
        List<StateMonth> stateMonths = hyComplaintsService.selectByMonth();
        List<StateMonth> stateMonths2 = hyComplaintsService.selectByMonth2();
        for (int i = 0; i < 12; i++) {
            BigDecimal decimal1 = new BigDecimal(stateMonths.get(i).getCount());
            BigDecimal decimal2 = new BigDecimal(stateMonths2.get(i).getCount());
            BigDecimal bigDecimal = decimal2.divide(decimal1,4, RoundingMode.HALF_UP);
            stateMonths.get(i).setCount(bigDecimal.doubleValue());
        }
        return stateMonths;
    }

    @RequestMapping("/selectActivity")
    public List<HyActivity> selectActivity() {
        QueryWrapper<HyActivity> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        List<HyActivity> list = hyActivityService.list(queryWrapper);
        List<HyActivity> hyActivities = list.stream().limit(10).collect(Collectors.toList());
        return hyActivities;
    }
    @RequestMapping("/selectRepairStream")
    public List<HyRepairStream> selectRepair() {
        QueryWrapper<HyRepairStream> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        List<HyRepairStream> list = hyRepairStreamService.list(queryWrapper);
        List<HyRepairStream> limit = list.stream().limit(10).collect(Collectors.toList());
        return limit;
    }
    @RequestMapping("/selectUser")
    public List<StateMonth> selectUser() {
        List<StateMonth> stateMonths = hyUserService.selectOrderByBuild();
        return stateMonths;
    }
    @RequestMapping("/selectOrderByBillType")
    public List<BillBean> selectOrderByBillType() {
        List<BillBean> list = hyBillService.selectOrderByBillType();
        return list;
    }
    @RequestMapping("/selectTu")
    public List<BillBean> selectTu() {
        return hyBillService.selectOrderBy();
    }

    @RequestMapping("/selectUserOrderByBillType")
    public List<BillBean> selectUserOrderByBillType(HttpSession session) {
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        List<BillBean> list = hyBillService.selectUserOrderByBillType(hyUser.getId());
        if(list.get(0)==null){
            BillBean billBean = new BillBean("0", 0, 0, 0, 0);
            list.set(0,billBean);
        }
        return list;
    }

    @RequestMapping("/selectUserTu")
    public List<BillBean> selectUserTu(HttpSession session) {
        HyUser hyUser = (HyUser)session.getAttribute("hyUser");
        List<BillBean> list = hyBillService.selectUserOrderBy(hyUser.getId());
        if(list.size()==0){
            for (int i = 0; i < 12; i++) {
                BillBean billBean = new BillBean("0", 0, 0, 0, 0);
                list.add(billBean);
            }
            return list;
        }else{
            if(list.get(0).getName()==null){
                return list;
            }else{
                List<BillBean> list2 = new ArrayList<>();
                for (int i = 0; i < 12; i++) {
                    BillBean billBean = new BillBean("0", 0, 0, 0, 0);
                    list2.add(billBean);
                }
                for (BillBean billBean : list) {
                    String name = billBean.getName();
                    int ss = Integer.parseInt(name);
                    ss=ss-1;
                    list2.set(ss,billBean);
                }
                return list2;
            }
        }

    }


}
